package com.english.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.english.entity.CourseSchedule;
import com.english.mapper.CourseScheduleMapper;
import com.english.service.CourseScheduleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CourseScheduleServiceImpl extends ServiceImpl<CourseScheduleMapper, CourseSchedule> implements CourseScheduleService {

    @Override
    public String addCourseSchedule(Date date, String imageUrl, Long classId) {
        // 创建课程表对象
        CourseSchedule courseSchedule = new CourseSchedule();

        // 直接设置日期
        courseSchedule.setWeek(date);

        // 设置图片链接
        courseSchedule.setImageUrl(imageUrl);

        // 设置班级ID
        courseSchedule.setClassId(classId);

        // 设置当前时间为上传时间
        courseSchedule.setUploadTime(new Date());

        // 保存到数据库
        boolean result = save(courseSchedule);

        return result ? "添加成功" : "添加失败";
    }

    @Override
    public List<CourseSchedule> getAllCourseSchedules() {
        // 查询所有课程安排，按上传时间倒序排列
        List<CourseSchedule> courseSchedules = lambdaQuery()
                .orderByDesc(CourseSchedule::getUploadTime)
                .list();
        
        // 处理图片URL，将相对路径转换为完整URL
        for (CourseSchedule courseSchedule : courseSchedules) {
            if (courseSchedule.getImageUrl() != null && !courseSchedule.getImageUrl().isEmpty()) {
                String imageUrl = courseSchedule.getImageUrl();
                if (!imageUrl.startsWith("http")) {
                    imageUrl = "http://localhost:8080" + imageUrl;
                    courseSchedule.setImageUrl(imageUrl);
                }
            }
        }
        
        return courseSchedules;
    }

    @Override
    public List<CourseSchedule> getCourseScheduleByClassId(Long classId) {
        // 验证班级ID
        if (classId == null || classId <= 0) {
            throw new IllegalArgumentException("班级ID无效");
        }

        // 查询数据库
        List<CourseSchedule> courseSchedules = lambdaQuery()
                .eq(CourseSchedule::getClassId, classId)
                .list();
        
        // 处理图片URL，将相对路径转换为完整URL
        for (CourseSchedule courseSchedule : courseSchedules) {
            if (courseSchedule.getImageUrl() != null && !courseSchedule.getImageUrl().isEmpty()) {
                String imageUrl = courseSchedule.getImageUrl();
                if (!imageUrl.startsWith("http")) {
                    imageUrl = "http://localhost:8080" + imageUrl;
                    courseSchedule.setImageUrl(imageUrl);
                }
            }
        }
        
        return courseSchedules;
    }

    @Override
    public String updateCourseScheduleById(CourseSchedule courseSchedule) {
        // 设置当前时间为修改时间
        courseSchedule.setUploadTime(new Date());

        // 更新数据库
        boolean result = updateById(courseSchedule);

        return result ? "修改成功" : "修改失败";
    }

    @Override
    public String deleteCourseScheduleById(Long id) {
        boolean result = removeById(id);
        return result ? "删除成功" : "删除失败";
    }

    @Override
    public List<CourseSchedule> getCourseScheduleByDate(Date date) {
        // 验证日期
        if (date == null) {
            throw new IllegalArgumentException("日期参数不能为空");
        }

        // 创建日历对象，设置时间为传入日期的开始
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date startDate = calendar.getTime();

        // 设置时间为传入日期的结束
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        Date endDate = calendar.getTime();

        // 查询数据库
        return lambdaQuery()
                .ge(CourseSchedule::getWeek, startDate)
                .le(CourseSchedule::getWeek, endDate)
                .list();
    }
}
